package com.dc.bobaoge.project.v3.to.v2.business.core;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.bobaoge.base.entity.message.common.po.MessageCollectionUserHistory;
import com.dc.bobaoge.base.mapper.message.MessageCollectionUserHistoryMapper;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreMessageCollection;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreMessageCollectionMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.entity.tuple.Tuple;
import com.own.entity.tuple.Tuple2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * MessageCollectionResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-13 14:53:54
 */
@Slf4j
@Component
public class MessageCollectionResolveMethod implements ResolveMethod {

    @Resource
    private MessageCollectionUserHistoryMapper messageCollectionUserHistoryMapper;

    @Resource
    private OldCoreMessageCollectionMapper oldCoreMessageCollectionMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        var history = getHistory(trigger.getTargetId(), trigger.getSharding());
        if (history == null) {
            throw new BusinessSimpleException("未查询到收藏的信息");
        }
        // 查询用户在旧版上有没有收藏过
        var wrapper = new LambdaQueryWrapper<OldCoreMessageCollection>()
                .eq(OldCoreMessageCollection::getUserId, history.getFkUserId())
                .eq(OldCoreMessageCollection::getMessageId, history.getFkMessageId())
                .last("limit 1");
        var item = oldCoreMessageCollectionMapper.selectOne(wrapper);
        // 取消收藏，旧版数据不存在的情况
        if (history.getIsDeleted() == 1 && item == null) {
            return;
        }
        // 取消收藏，旧版数据存在的情况
        if (history.getIsDeleted() == 1 && item != null) {
            // 判断状态是否一致
            if (item.getCollectionType() == 0) {
                return;
            }
            // 删除
            item.setCollectionType(0);
            item.setCollectionTime(history.getGmtModified());
            oldCoreMessageCollectionMapper.updateById(item);
            return;
        }
        if (item == null) {
            // 新建
            var collection = new OldCoreMessageCollection();
            collection.setCollectionTime(history.getGmtCreate());
            collection.setMessageId(history.getFkMessageId());
            collection.setUserId(history.getFkUserId());
            collection.setCollectionType(1);
            oldCoreMessageCollectionMapper.insert(collection);
        } else {
            if (item.getCollectionType() == 1) {
                return;
            }
            // 已经是收藏的状态了
            item.setCollectionType(1);
            item.setCollectionTime(history.getGmtCreate());
            oldCoreMessageCollectionMapper.updateById(item);
        }
    }

    /**
     * 根据季度集群获取信息
     *
     * @param id       主键id
     * @param sharding 分片键
     * @return 信息
     */
    private MessageCollectionUserHistory getHistory(Long id, String sharding) {
        var shardingTuple = resolveSharding(sharding);
        var messageType = shardingTuple.value1();
        var messageQuarter = shardingTuple.value2();
        return messageCollectionUserHistoryMapper.selectOneWithoutLogicDelete(id, messageType, messageQuarter);
    }

    /**
     * 处理分片键
     *
     * @param sharding 分片键
     * @return 分片键
     */
    private Tuple2<String, String> resolveSharding(String sharding) {
        try {
            var array = sharding.split("_");
            return Tuple.of(array[0], array[1]);
        } catch (Exception e) {
            throw new BusinessSimpleException("分片键格式不正确");
        }
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "message_collection_user_history";
    }

}
